package test5.lupeng.jf.com.teamdemo.activitys;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.MyMessageAdapter;
import test5.lupeng.jf.com.teamdemo.bean.request.MyShareBean;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseMyMessage;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;
import test5.lupeng.jf.com.teamdemo.untils.UserInfo;

public class MyMessagesActivity extends AppCompatActivity {
    ArrayList<ResponseMyMessage.ResultBean> beans = new ArrayList<>();
    MyMessageAdapter adapter;
    @BindView(R.id.rl)
    RecyclerView rl;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_messages);
        ButterKnife.bind(this);
        initView();
        getDataMessages(lasttimestamp);
    }

    public void initView() {
        adapter = new MyMessageAdapter(beans);
        rl.setAdapter(adapter);
        rl.setLayoutManager(new LinearLayoutManager(this));
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("我的消息");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                lasttimestamp=0;
                beans.clear();
                getDataMessages(lasttimestamp);
            }
        });
        refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                //不对
//                lasttimestamp=beans.get(beans.size()-1).getArticle().getTimestamp();
//                getDataMessages(lasttimestamp);
            }
        });

    }

    long lasttimestamp = 0;

    public void getDataMessages(long timestamp) {
        String url = "https://leancloud.cn:443/1.1/functions/getMeMessage";
        OkHttpClient client = new OkHttpClient();
        Request request = HTTPUtils.requestPost(new MyShareBean(UserInfo.userId, 10, timestamp), url);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.i("GG", "onResponse: " + result);
                ResponseMyMessage responseMyMessage = new Gson().fromJson(result, ResponseMyMessage.class);
                beans.addAll(responseMyMessage.getResult());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });


            }
        });

    }


}
